package top.broncho.lottery.ui.base

import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import top.broncho.lottery.di.Injectable
import java.lang.reflect.ParameterizedType
import javax.inject.Inject

abstract class BaseDiFragment<VM : ViewModel> : BaseFragment(), Injectable {

    @Inject
    lateinit var viewModelFactory: ViewModelProvider.Factory

    protected lateinit var viewModule: VM

    override fun onAttach(context: Context) {
        super.onAttach(context)
        val entityClass = (javaClass.genericSuperclass as ParameterizedType)
            .actualTypeArguments[0] as Class<VM>
        viewModule = ViewModelProviders.of(activity!!, viewModelFactory).get(entityClass)
    }

}